export const debounce = function (fn, wait = 500, isImmediate = false) {
  var timerId = null;
  var flag = true;
  return function () {
    var context = this
    var args = arguments
    clearTimeout(timerId)
    if (isImmediate) {
      if (flag) {
        fn.apply(context, args)
        flag = false
      }
      timerId = setTimeout(function () {
        flag = true
      }, wait)
    } else {
      timerId = setTimeout(function () {
        fn.apply(context, args)
      }, wait)
    }
  }
}

export const throttle = function (fn, wait = 500, isImmediate = false) {
  var flag = true;
  return function () {
    if (flag == true) {
      var context = this
      var args = arguments
      flag = false
      isImmediate && fn.apply(context, args)
      setTimeout(() => {
        !isImmediate && fn.apply(context, args)
        flag = true
      }, wait)
    }
  }
}